/*
 * @[H]:  Copyright (c) 2021 Phytium Information Technology, Inc. 
 * 
 *  SPDX-License-Identifier: Apache-2.0. 
 * 
 * @Date: 2021-07-12 18:43:06
 * @LastEditTime: 2021-07-15 10:45:39
 * @Description:  Description of file
 * @Modify History: 
 * * * Ver   Who        Date         Changes
 * * ----- ------     --------    --------------------------------------
 */

#ifndef BSP_DRIVER_GPIO_F_GPIO_H
#define BSP_DRIVER_GPIO_F_GPIO_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "ft_types.h"
#include "parameters.h"
#include "ft_assert.h"

    // #define ERR_INPUT_BAUD_NO_SUPPORT FT_CODE_ERR(ErrModBsp, ErrBspGpio, 0x1u)

#define F_GPIO_DIRECTION_INPUT 0
#define F_GPIO_DIRECTION_OUTPUT 1

#define F_GPIO_A_PIN0 0
#define F_GPIO_A_PIN1 1
#define F_GPIO_A_PIN2 2
#define F_GPIO_A_PIN3 3
#define F_GPIO_A_PIN4 4
#define F_GPIO_A_PIN5 5
#define F_GPIO_A_PIN6 6
#define F_GPIO_A_PIN7 7
#define F_GPIO_B_PIN0 8
#define F_GPIO_B_PIN1 9
#define F_GPIO_B_PIN2 10
#define F_GPIO_B_PIN3 11
#define F_GPIO_B_PIN4 12
#define F_GPIO_B_PIN5 13
#define F_GPIO_B_PIN6 14
#define F_GPIO_B_PIN7 15

    typedef enum
    {
        F_GPIO_OUTPUT_LOW = 0,
        F_GPIO_OUTPUT_HIGH = 1,
    } FGpioOutLevel;

    typedef enum
    {
        F_GPIO_IRQ_TYPE_EDGE_FALLING = 0,
        F_GPIO_IRQ_TYPE_EDGE_RISING,
        F_GPIO_IRQ_TYPE_LEVEL_LOW,
        F_GPIO_IRQ_TYPE_LEVEL_HIGH
    } FGpioIrqType;

    typedef struct
    {
        u32 instance_id; /* Id of device*/
        uintptr_t base_address;
        u32 irq_num;
        u8 gpio_group[F_GPIO_GROUP_NUM];
    } FGpioConfig;

    typedef struct
    {
        u32 is_ready; /* Device is ininitialized and ready*/
        FGpioConfig config;
    } FGpio;

    void FGpioPinIrqMask(FGpio *instance_p, u32 pin);
    void FGpioPinIrqUnmask(FGpio *instance_p, u32 pin);
    void FGpioPinIrqSetType(FGpio *instance_p, u32 pin, FGpioIrqType type);
    void FGpioPinIrqAck(FGpio *instance_p, u32 pin);
    u32 FGpioPinIrqStatusGet(FGpio *instance_p);

    void FGpioSetDirection(FGpio *instance_p, u32 pin, fsize_t direction);
    fsize_t FGpioGetDirection(FGpio *instance_p, u32 pin);
    void FGpioSetValue(FGpio *instance_p, u32 pin, FGpioOutLevel Level);
    fsize_t FGpioGetValue(FGpio *instance_p, u32 pin);

    FGpioConfig *FGpioLookupConfig(u32 instance_id);
    ft_error_t FGpioCfgInitialize(FGpio *instance_p, FGpioConfig *config_p);
#ifdef __cplusplus
}
#endif

#endif // !
